package com.lwj.liwanjia.view.activity.myindent.tuikuantuihuo;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.MyIndentBean;
import com.lwj.liwanjia.bean.WuliuBean;
import com.lwj.liwanjia.impl.WuliuView;
import com.lwj.liwanjia.presenter.WuliuPresenter;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.utils.TuihuoDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TxWlActivity extends BaseActivity implements WuliuView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_shangpin)
    ImageView ivShangpin;
    @BindView(R.id.tv_shangpin)
    TextView tvShangpin;
    @BindView(R.id.tv_jianjie)
    TextView tvJianjie;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_name)
    EditText edName;
    @BindView(R.id.tv_phone)
    EditText edPhone;
    @BindView(R.id.ed_address)
    EditText edAddress;
    @BindView(R.id.tv_wuliu_gongsi)
    TextView tvWuliuGongsi;
    @BindView(R.id.tv_wuliu_danhao)
    EditText tvWuliuDanhao;
    @BindView(R.id.tv_wuliu_beizhu)
    EditText tvWuliuBeizhu;

    private List<String> dialogList = new ArrayList<>();
    private String token;
    private WuliuPresenter presenter;
    private Map<String, Object> map = new HashMap<>();
    private String tradeId;

    @Override
    protected void initView() {
        MyIndentBean.DataBean.GoodsBean goods = (MyIndentBean.DataBean.GoodsBean) getIntent().getSerializableExtra("data");
        tradeId = goods.getTradeId();
        MyIndentBean.DataBean.GoodsBean.CommodityBean commodityBean = goods.getCommodity().get(0);
        Glide.with(this).load(commodityBean.getCommodityimage()).error(R.mipmap.tupian).into(ivShangpin);
        tvShangpin.setText(commodityBean.getCommodityname());
        tvJianjie.setText(commodityBean.getCommodityinfo());
        tvPrice.setText("￥" + commodityBean.getCommodityprice());
        tvNum.setText("*" + commodityBean.getCommodityNum());
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("填写物流");
        presenter = new WuliuPresenter(this);
        token = SpfUtils.getSpfUtils(this).getToken();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tx_wl;
    }

    @OnClick({R.id.tv_left, R.id.rl_gongsi, R.id.tv_queren})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.rl_gongsi://选择物流公司
                dialogList.clear();
                dialogList.add("顺丰快递");
                dialogList.add("圆通快递");
                dialogList.add("中通快递");
                dialogList.add("天天快递");
                dialogList.add("申通快递");
                dialogList.add("韵达快递");
                dialogList.add("EMS");
                dialogList.add("德邦快递");
                TuihuoDialog dialog = new TuihuoDialog(this, tvWuliuGongsi, "物流公司", dialogList);
                dialog.show();
                break;
            case R.id.tv_queren://确认
                String name = edName.getText().toString();
                String phone = edPhone.getText().toString();
                String address = edAddress.getText().toString();
                String wlDanhao = tvWuliuDanhao.getText().toString();
                String gongsi = tvWuliuGongsi.getText().toString();
                String beizhu = tvWuliuBeizhu.getText().toString();
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(wlDanhao) || TextUtils.isEmpty(gongsi)) {
                    Toast.makeText(this, "关键数据不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                map.put("token", token);
                map.put("returnUserName", name);
                map.put("returnTel", phone);
                map.put("returnCompany", gongsi);
                map.put("returnOrderId", tradeId);
                map.put("returnLogistics", wlDanhao);
                if (!TextUtils.isEmpty(address)) {
                    map.put("returnAddress", address);
                }
                if (!TextUtils.isEmpty(beizhu)) {
                    map.put("returnMark", beizhu);
                }
                presenter.editWuliu(map);
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载有误", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void editWuliu(WuliuBean wuliuBean) {
        if (wuliuBean.getState() == 0) {
            Toast.makeText(this, "填写物流失败", Toast.LENGTH_SHORT).show();
        } else if (wuliuBean.getState() == 1) {
            if (wuliuBean.getData().getStatus().equals("物流填写成功")) {
                Toast.makeText(this, "物流填写成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter = null;
        }
    }
}
